using System;
using System.Data;
using GoodsInvoicing.BLL.Service;
using GoodsInvoicing.Views.View;
using GoodsInvoicing.Model.UserRole;

namespace GoodsInvoicing.Presentions.Presenter
{
    /// <summary>
    /// 表[GI_User]的Presenter实现类
    /// </summary>
    public class UserListFormPresenter : IUserListFormPresenter
    {
        private UserListFormPresenter()
        {
            throw new NotSupportedException("无接口的实现.");
        }

        public UserListFormPresenter(IUserListForm view, IGI_UserService userService)
        {
            this.view = view;
            this.userService = userService;
        }

        #region 属性
        private IUserListForm view;
        public IUserListForm View
        {
            get{ return view;}
        }

        private IGI_UserService userService;
        public IGI_UserService UserService
        {
            get { return userService; } 
        }
        #endregion 属性

        #region 方法
        /// <summary>
        /// 初始化视图
        /// </summary>
        public void InitializeView()
        {
            this.View.UserListFormLoad += new EventHandler<EventArgs>(View_UserListFormLoad);
            this.View.SearchButtonClick += new EventHandler<EventArgs>(View_SearchButtonClick);

            this.View.UserDeleteClick += new EventHandler<EventArgs>(View_UserDeleteClick);
        }

        #endregion

        private void View_UserListFormLoad(object sender, EventArgs e) 
        {
            this.View.UserList = UserService.GetAll();
        }

        private void View_SearchButtonClick(object sender, EventArgs e) 
        {
            GI_User user = new GI_User();
            user.UserName = this.View.SearchUserName;
            this.View.UserList = UserService.Search(user);
        }

        public void View_UserDeleteClick(object sender, EventArgs e) 
        {
            //GI_User user = new GI_User();
            int userid = this.View.DeleteUserID;
            UserService.Delete(UserService.Get(userid));
        }
    }
}

